package com.ruoyi.system.controller;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TbLargScreenParamet;
import com.ruoyi.system.service.ITbLargScreenParametService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 大屏相关配置参数Controller
 * 
 * @author nonghg
 * @date 2022-01-19
 */
@Controller
@RequestMapping("/system/parameter")
public class TbLargScreenParametController extends BaseController
{
    private String prefix = "system/parameter";

    @Autowired
    private ITbLargScreenParametService tbLargScreenParametService;

    @RequiresPermissions("system:parameter:view")
    @GetMapping()
    public String parameter()
    {
        return prefix + "/parameter";
    }

    /**
     * 查询大屏相关配置参数列表
     */
    @RequiresPermissions("system:parameter:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TbLargScreenParamet tbLargScreenParamet)
    {
        startPage();
        List<TbLargScreenParamet> list = tbLargScreenParametService.selectTbLargScreenParametList(tbLargScreenParamet);
        return getDataTable(list);
    }

    /**
     * 导出大屏相关配置参数列表
     */
    @RequiresPermissions("system:parameter:export")
    @Log(title = "大屏相关配置参数", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TbLargScreenParamet tbLargScreenParamet)
    {
        List<TbLargScreenParamet> list = tbLargScreenParametService.selectTbLargScreenParametList(tbLargScreenParamet);
        ExcelUtil<TbLargScreenParamet> util = new ExcelUtil<TbLargScreenParamet>(TbLargScreenParamet.class);
        return util.exportExcel(list, "大屏相关配置参数数据");
    }

    /**
     * 新增大屏相关配置参数
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存大屏相关配置参数
     */
    @RequiresPermissions("system:parameter:add")
    @Log(title = "大屏相关配置参数", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TbLargScreenParamet tbLargScreenParamet)
    {
        TbLargScreenParamet tb=new TbLargScreenParamet();
        tb.setName(tbLargScreenParamet.getName());
        if(tbLargScreenParametService.selectTbLargScreenParametList(tb).isEmpty()){
            tbLargScreenParamet.setCreateBy(ShiroUtils.getSysUser().getLoginName());
            tbLargScreenParamet.setCreateTime(new Date());
            return toAjax(tbLargScreenParametService.insertTbLargScreenParamet(tbLargScreenParamet));
        }else {
            return AjaxResult.error("已存在该参数，请确认！");
        }

    }

    /**
     * 修改大屏相关配置参数
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TbLargScreenParamet tbLargScreenParamet = tbLargScreenParametService.selectTbLargScreenParametById(id);
        mmap.put("tbLargScreenParamet", tbLargScreenParamet);
        return prefix + "/edit";
    }

    /**
     * 修改保存大屏相关配置参数
     */
    @RequiresPermissions("system:parameter:edit")
    @Log(title = "大屏相关配置参数", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TbLargScreenParamet tbLargScreenParamet)
    {
        TbLargScreenParamet tb=new TbLargScreenParamet();
        tb.setName(tbLargScreenParamet.getName());
        List<TbLargScreenParamet> tbLargScreenParametList=tbLargScreenParametService.selectTbLargScreenParametList(tb);
        TbLargScreenParamet oldParamet=tbLargScreenParametList.get(0);
        if(oldParamet.getId().equals(tbLargScreenParamet.getId())){
            tbLargScreenParamet.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
            tbLargScreenParamet.setUpdateTime(new Date());
            return toAjax(tbLargScreenParametService.updateTbLargScreenParamet(tbLargScreenParamet));
        }else{
            return AjaxResult.error("该参数已存在，请确认！");
        }

    }

    /**
     * 删除大屏相关配置参数
     */
    @RequiresPermissions("system:parameter:remove")
    @Log(title = "大屏相关配置参数", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tbLargScreenParametService.deleteTbLargScreenParametByIds(ids));
    }
}
